package cn.itsource.org.service.impl;

import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import cn.itsource.basic.utli.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    //redis
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Transactional
    public void delect(Long id) {
        departmentMapper.delect(id);
        redisTemplate.opsForValue().set("deptTree",null);
        this.queryTree();
    }

    @Override
    @Transactional
    public void batchDeletedelect(List<Long> ids) {
        departmentMapper.batchDeletedelect(ids);
        redisTemplate.opsForValue().set("deptTree",null);
        this.queryTree();
    }
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        //更新数据库
        this.update(department);

    }

    @Override
    @Transactional
    public void update(Department department) {
        //1.设置dirpath
        String dirPath="";
        if(department.getParent()==null){//说明是顶级部门
            //通过自增主键把自己的id赋值到dirPath上
            dirPath="/"+department.getId();
        }else {//说明有父级部门，
            //获取父级部门的dirpath,然后加上自己的id
            Department Parent = departmentMapper.selectByid(department.getParent().getId());
            dirPath=Parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
        redisTemplate.opsForValue().set("deptTree",null);
        this.queryTree();
    }

    //查询部门树
    @Override
    public List<Department> queryTree() {
        List<Department> topDept= new ArrayList<>();
        /*System.out.println(redisTemplate.opsForValue().get("deptTree"));
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);*/
        //redisTemplate.expire("deptTree",3000, TimeUnit.SECONDS);
        //System.out.println(keys);
        if(redisTemplate.opsForValue().get("deptTree")==null){
            //1.查询所有部门
            List<Department> all = departmentMapper.selectAll();
            Map<Long,Department> map =new HashMap<>();
            //循环部门信息 存入map中
            for (Department d:all){
                //通过id所有部门信息封装到map中
                map.put(d.getId(),d);
            }
            //2.找到顶级部门
            for (Department dept:all){
                //通过遍历和条件找到顶级部门，并且封装到List中
                if(dept.getParent_id()==null){
                    topDept.add(dept);
                }else {//不是顶级部门的，就找出自己的上级部门,把自己设置到他的 children 属性上
                    //dept.getParent_id()通过自己的Parent_id找到上级id
                    Department parent = map.get(dept.getParent_id());
                    //判断上级部门是否为空
                    if(parent!=null){
                        parent.getChildren().add(dept);
                    }
                }
            }
            redisTemplate.opsForValue().set("deptTree",topDept);
            //System.out.println(topDept);
            return topDept;
        }else {
            List<Department> list = (List)redisTemplate.opsForValue().get("deptTree");
            System.out.println("................");
            System.out.println(list);
            return list;
        }
    }
}
